<?php 
require_once 'chat.php';

//seconds required to audohide submit response messages
define('submit_message_timeout',10,true);//10s


	
	//function to redirect a page within 4 seconds
	//displays a redirect message
	function redirect($pagename,$url){
		echo "<p>Bringing you to <a href='$url' target=_self>$pagename</a> in <span id=seconds>4</span> seconds...</p>";
		echo "<script type='text/javascript'>";
		echo "var s=4;";
		echo "setInterval(\"if(s>0){s--;\$('#seconds').html(s);}\",1000);";
		echo "setTimeout(\"window.location='$url';\",4000);";
		echo "</script>";
	}


require 'template/header.php';

	switch ($page){
		case null:
		case page_home:
			echo "<h2>Warmest welcome to our sudoku-hub!</h2>
				<p class=graytext>In this site, you could play 5 levels of sudoku to your heart's content.</p>
				<p>This is a site built as part of my training and so, please bear with
				<br>any bugs that you might possibly find.</p>
				<p class=graytext>To make things short..., hit the Play Now! button on top to start solving a sudoku.</p>
				<p>Hang on! I forgot to mention that you will have to do a quick 30 seconds registration first
				<br>before starting. It won't take more than 30 seconds. Trust me! :)</p>
				<p class=graytext>Hope you enjoy sudoku better here :)</p>
					";
			
			break;
			
		case page_signin:
			echo "<table class=marginauto><tr><td>";
			echo "<h2>Please sign in...</h2>";
			echo "<div><form action='$user_trysignin' method=post>
			<table class=smalltable><tr>
				<td>Login name</td>
				<td><input name=login_user type=text></td>
			</tr><tr>
				<td>Password</td>
				<td><input name=login_pass type=password></td>
			</tr>
			<tr><td colspan=2 class=right><input type=submit value='Sign in'></td></table></form></div>
			<p>Don't have an account yet? <a href='$page_signup'>Get one for free here!</a></p>";
			echo "</td></tr></table>";
			break;
			
		case page_signinsuccess:
			echo "Sign in successful!";
			redirect("game rooms",$game_joinrooms);
			break;
			
		case page_signup:
			echo "<h2>You are a few seconds away from playing Sudoku!";
				echo "<table class=marginauto><tr><td>";
			echo "<div><form id=signupform action='$user_tryregister' method=post>
			<table class=smalltable><tr>
				<td>Nick name</td>
				<td><input name=reg_nickname type=text maxlength=32></td>
			</tr><tr>
				<td>Login name</td>
				<td><input name=reg_loginuser type=text maxlength=32></td>
			</tr><tr>
				<td>Password</td>
				<td><input id=password name=reg_password type=password maxlength=32></td>
			</tr><tr>
				<td>Confirm password</td>
				<td><input id=confirm name=reg_confirm type=password maxlength=32></td>
			</tr>
			<tr><td colspan=2 class=right>
				<input type=submit value='Thats it!'></td></table></form></div>";
			echo "</td></tr></table>";
			break;
			
		case page_signupsuccess:
			echo "<p>Great! Registration successful.</p>" .
				 "<p>No hassle and no email validation required.</p>" .
				 "<p>We have logged you in!</p>";
			redirect("game rooms",$game_joinrooms);
			break;		
		
		case page_profile:
			//echo "<table class=marginauto><tr><td>";
			include 'profile.php';
			//echo "</td></tr></table>";
			break;
			
		case page_editprofile:
			$userid=$user['id'];
			echo "<a href='$page_profile&profile=$userid' target=_self>View my own profile</a>";
			echo "<h2>Profile</h2>";
			echo "<div><form action='$user_tryupdate' method=post>
					<table class=smalltable><tr>
					<td>Login name</td>
					<td><input disabled type=text value='".$user['loginname']."' maxlength=32>
						<input type=hidden name=upd_loginname value='".$user['loginname']."'></td>
					</tr><tr>
					<td>Nick name</td>
					<td><input name=upd_nickname type=text value='".$user['nickname']."' maxlength=32></td>
					</tr>
					<tr><td colspan=2><a href='$page_changepassword'>Like to change password?</a></td></tr>
					<tr class=right><td colspan=2>
						<input type=submit value='Change'></td></tr>
					</table></form></div>";
			break;

		case page_changepassword:
			echo "<a href='$page_editprofile'>Go back to profile</a>";
			echo "<h2>Change password</h2>";
			echo "<div><form action='$user_trychangepassword' method=post>
					<table class=smalltable><tr><td>New password</td>
						<td><input type=password name=change_password maxlength=32></td></tr>
					<tr><td>Confirm password</td>
						<td><input type=password name=confirm_password maxlength=32></td></tr>
					<tr><td class=right colspan=2><input type=submit value='Change'></td></tr></table></form></div>";
			break;
		case page_gamerooms:
			echo "<h1 class=center><a href='$page_creategame'>Create new game!</a></h1>";
			echo "<div id=gamerooms>";
			echo printActivegamesTable();
			echo "</div>";

			//prints the chat component in the document
			//passed chatbox id
			printChat("homechat");
			
			//prints the javascript to initialize components
			printActivegamesRefreshScript();
			printChatInitializeScript("homechat",$settings['home_chatid'],urlencode($user['nickname']));

			break;
		case page_creategame:
			echo "<h2>Right away, we will prepare a game room for you very soon.</h2>";
			echo "<div><form action='$game_trycreategame' method=post>
					<table><tr><td>Room name</td>
						<td><input type=text name=roomname maxlength=32></td></tr>
					<tr><td>Room description (optional)</td>
						<td><textarea name=description rows=4 cols=40></textarea></td></tr>
					<tr><td>Difficulty</td>
						<td><input type=radio name=difficulty value=1 checked>Supereasy</input><br>
							<input type=radio name=difficulty value=2>Easy</input><br>
							<input type=radio name=difficulty value=3>Medium</input><br>
							<input type=radio name=difficulty value=4>Hard</input><br>
							<input type=radio name=difficulty value=5>Insane</input></td></tr>
					<tr><td class=right colspan=2><input type=submit value='Get it done!'></td></tr></table></form></div>";
			break;
		case page_game:
			include 'ui.php';
			break;

		case page_howto:
			echo "<a href='http://www.google.com'>www.google.com</a>";
			break;
		case page_leaderboard:
						
			echo "<div id=leaderboardwrap>	
				<table class='bigtable marginauto'>
				<tr class=heading>
				<td>Rank</td>
				<td>Name</td>
				<td>Nickname</td>
				<td>Score</td>
				</tr>";

			$records=getLeaderboard();
			if ($records){
				foreach($records as $row){
					$userid=$row['userid'];
					$rank=$row['rank'];
					$name=$row['loginname'];
					$nick=$row['nickname'];
					$score=$row['score'];
					echo "<tr>";
					echo "<td>$rank</td>";
					echo "<td><a href='$page_profile&profile=$userid' target=_self>$name</a></td>";
					echo "<td>$nick</td>";
					echo "<td>$score</td>";
					echo "</tr>";
				}
			}
			echo "</table>
				<br>
				<p>Not refreshed? Hall of fame is updated every ten minutes. <br>
				We need to save some server processing power (and $$$ too!) :P </p>
				<p>Time remaining for next refresh " .
					(leaderboard_refresh_time-time()+$settings['leaderboardLF']). 
				" seconds.</p>
				</div>";
				
			printChat("leaderboardchat");
			
			//prints the javascript to initialize components
			printChatInitializeScript("leaderboardchat",$settings['leaderboard_chatid'],urlencode($user['nickname']));
			break;
		default :
			echo "The page you are looking for does not exist.";
			break;
	}

require 'template/footer.php';
?>
